<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>New Auction</title>
<script language="JavaScript" type="text/javascript">
function submitAuction(auctionid)
{
  var name = document.newAuctionForm.itemName.value;
  if (name == null || name == "")
	  {
	  	alert("Error: You must input a value for the auction name");
	  	return;
	  }
  var price = document.newAuctionForm.startingPrice.value;
  if (price - 0 != price)
	  {
	  	alert("Error: Starting Price must be a numeric value");
	  	return;
	  }
  var increment = document.newAuctionForm.increment.value;
  if (increment - 0 != increment)
	  {
	  alert("Error: Increment must be a numeric value");
	  return;
	  }
  if (increment < 1)
	  {
	  alert("Error: Increment must be at least $1.00");
	  return;
	  }
  var reserve = document.newAuctionForm.reserve.value;
  if (reserve != null && reserve != "")
	  {
	  	if (reserve - 0 != reserve)
	  		{
	  		alert("Error: Reserve must be a numeric value");
	  		return;
	  		}
	  	if (parseInt(reserve) < parseInt(price))
	  		{
	  		alert("Error: The reserve must be greater than the starting price");
	  		return;
	  		}
	  }
  var date = document.newAuctionForm.closeDate.value;
  if (!isDate(date, undefined,2, 1, 0))
	  {
	  alert("Close Date is invalid");
	  return;
	  }
  
  document.newAuctionForm.submit();
  
}

//Value parameter - required. All other parameters are optional.
function isDate(value, sepVal, dayIdx, monthIdx, yearIdx) {
    try {
        //Change the below values to determine which format of date you wish to check. It is set to dd/mm/yyyy by default.
        var DayIndex = dayIdx !== undefined ? dayIdx : 0; 
        var MonthIndex = monthIdx !== undefined ? monthIdx : 0;
        var YearIndex = yearIdx !== undefined ? yearIdx : 0;
 
        value = value.replace(/-/g, "/").replace(/\./g, "/"); 
        var SplitValue = value.split(sepVal || "/");
        var OK = true;
        if (!(SplitValue[DayIndex].length == 1 || SplitValue[DayIndex].length == 2)) {
            OK = false;
        }
        if (OK && !(SplitValue[MonthIndex].length == 1 || SplitValue[MonthIndex].length == 2)) {
            OK = false;
        }
        if (OK && SplitValue[YearIndex].length != 4) {
            OK = false;
        }
        if (OK) {
            var Day = parseInt(SplitValue[DayIndex], 10);
            var Month = parseInt(SplitValue[MonthIndex], 10);
            var Year = parseInt(SplitValue[YearIndex], 10);
 
            if (OK = ((Year > 1900) && (Year < new Date().getFullYear()+2))) {
                if (OK = (Month <= 12 && Month > 0)) {

                    var LeapYear = (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0));   
                    
                    if(OK = Day > 0)
                    {
                        if (Month == 2) {  
                            OK = LeapYear ? Day <= 29 : Day <= 28;
                        } 
                        else {
                            if ((Month == 4) || (Month == 6) || (Month == 9) || (Month == 11)) {
                                OK = Day <= 30;
                            }
                            else {
                                OK = Day <= 31;
                            }
                        }
                    }
                }
            }
        }
        return OK;
    }
    catch (e) {
        return false;
    }
} 
</script>
</head>
<body>
<div class="main">
  <div class="header">
    <div class="resize">
      <div class="logo"><img src="images/logo.png" alt="picture" width="171" height="42" border="0" /></a></div>
      <div class="clr"></div>
    </div>
    <div class="clr"></div>
  </div>
  <div class="slider_resize">
    <div class="clr"></div>
  </div>
  <div class="FBG">
    <div class="clr"></div>
  </div>
  <div class="body">
<div class="resize">
      <div class="left">
<h3>New Auction</h3>
<% 
	String mysJDBCDriver = (String)session.getValue("mysJDBCDriver"); 
	String mysURL = (String)session.getValue("mysURL"); 
   	String mysUserID = (String)session.getValue("mysUserID");
  	String mysPassword = (String)session.getValue("mysPassword");
	      
     String username = ""+session.getValue("login");
	java.sql.Connection conn=null;
	try 
	{
       	Class.forName(mysJDBCDriver).newInstance();
		java.util.Properties sysprops=System.getProperties();
		sysprops.put("user",mysUserID);
		sysprops.put("password",mysPassword);
	     
		//connect to the database
		conn=java.sql.DriverManager.getConnection(mysURL,sysprops);
		System.out.println("Connected successfully to database using JConnect");

		java.sql.Statement stmt1=conn.createStatement();
		java.sql.Statement stmt2=conn.createStatement();
	       			
		java.sql.ResultSet itemTypes = stmt1.executeQuery("select distinct Item.Type from Item");
%>
		<form name="newAuctionForm" method="post" action="SubmitNewAuction.jsp">
			<table style="text-align: left">
				<tr style="text-align: left">
					<td >Item Name: </td>
					<td><input type="text" name="itemName"/></td>
				</tr>
				<tr>
					<td>Item Year: </td>
					<td><input type="text" name="itemYear"/></td>
				</tr>
				<tr>
					<td>Item Type</td>
					<td>
						<select name="itemType">
							<option value="Car">Car</option>
							<option value="DVD">DVD</option>
							<option value="Electronics">Electronics</option>
							<option value="Clothing & Jewelry">Clothing & Jewelry</option>
							<option value="Sporting Goods">Sporting Goods</option>
							<option value="Collectables">Collectables</option>
							<option value="Home & Garden">Home & Garden</option>						
						</select>
					</td>
				</tr>
				<tr>
					<td>Starting Price: </td>
					<td><input type="text" name="startingPrice"/></td>
				</tr>
				<tr>
					<td>Increment: </td>
					<td><input type="text" name="increment"/></td>
				</tr>
				<tr>
					<td>Reserve: </td>
					<td><input type="text" name="reserve"/></td>
				</tr>
				<tr>
					<td>Close Date: </td>
					<td><input type="date" name="closeDate"/></td>
				</tr>
			</table>
		</form>
		
		<button onclick="submitAuction()">Submit</button>
		<br/><br/>
<% 
String status = (String)session.getValue("status");
if (status.equals("manager"))
{
	   %><input type="button" value="Home" onclick="window.open('ManagerHomePage.jsp','_self');" /><%
}
else if (status.equals("employee"))
{
	  %><input type="button" value="Home" onclick="window.open('EmployeeHomePage.jsp','_self');" /><%
}
else
{
	  %><input type="button" value="Home" onclick="window.open('CustomerHomePage.jsp','_self');" /><%
}

%><input type="button" value="Logout" onclick="window.open('index.htm','_self');" /><%
	} catch(Exception e)
	{
		e.printStackTrace();
		out.print(e.toString());
	}
	finally{
	
		try{conn.close();}catch(Exception ee){};
	}
%>
</div>
</div>
<div class="clr"></div>
  </div>
  <div class="footer">
    <div class="resize" style="background:#141414;">
				<p class="leftt">
					Created by: Andrew Gratta, Sarah Jordan, and Kevin O'Flaherty<br /></p>
      <p class="rightt"><span class="leftt">Copyright. All Rights Reserved</span></p>
      <p class="rightt">Developed for CSE305 at Stony Brook University</p>
      <div class="clr"></div>
    </div>
  </div>
</div>
</body>
</html>